<%@page import="br.com.maternity.componentes.grid.PersonalGrid"%>
<%@page import="br.com.maternity.componentes.grid.PersonalGridStyle"%>
<%@page import="br.com.maternity.componentes.grid.PersonalGridColumn"%>
<%@page import="br.com.maternity.classes.entity.AtendimentoExame"%>
<%@page import="br.com.maternity.classes.entity.Medico"%>
<%@page import="java.util.List"%>
<%@page import="br.com.maternity.classes.entity.Usuario"%>
<%@page import="br.com.maternity.classes.dao.ProcedimentoDAO"%>
<%@page import="br.com.maternity.classes.dao.ClinicaDAO"%>
﻿<%-- 
    Document   : triagem
    Created on : Nov 17, 2012, 1:15:47 PM
    Author     : Gattini
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="br.com.maternity.classes.dao.MedicoDAO"%>
<%@page import="br.com.maternity.componentes.dropdownlist.DropDownList"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="br.com.maternity.classes.dao.AtendimentoDAO"%>
<%@page import="br.com.maternity.classes.entity.Atendimento"%>
<%@page import="br.com.maternity.util.Parser"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:include page="../cabecalho.jsp"></jsp:include>
<%
    String id, paciente = "", data = "", responsavel = "", servletUrl = request.getContextPath() + "/avaliacaoMedica";
    String sintomas = "", diagnostico = "", carater = "", condicao = "";
    Integer crm = 0;
    List<AtendimentoExame> exames = null;
    id = Parser.parseString(request.getParameter("atendimento"));
    if (id.isEmpty()) {
        response.getWriter().write("<script>window.location.href='" + request.getContextPath() + "/relatorios/listagemAvaliacao.jsp';</script>");
    } else {
        Atendimento a = new AtendimentoDAO().getById(Integer.parseInt(id));
        if (a == null) {
            response.getWriter().write("<script>window.location.href='" + request.getContextPath() + "/relatorios/listagemAvaliacao.jsp';</script>");
        } else {
            paciente = a.getPacienteNome();
            responsavel = a.getResponsavelNome();
            data = Parser.parseDateToString(a.getPaciente().getDataNascimento());
            sintomas = a.getSintomas() == null ? "" : a.getSintomas();
            diagnostico = a.getDiagnosticoInicial() == null ? "" : a.getDiagnosticoInicial();
            carater = a.getCaraterInternacao() == null ? "" : a.getCaraterInternacao();
            condicao = a.getCondicaoInternacao() == null ? "" : a.getCondicaoInternacao();
            crm = a.getCrmMedico();
            exames = a.getExames();
        }
    }
%>
<div id="titulo">Avaliacao Medica</div>
<div id="erro"></div>
<div id="painel">    
    <form id="frmAvaliacao" name="frmAvaliacao" method="POST" action="avaliacaoMedica">
        <input type="hidden" name="hdfId" id="hdfId" value="<%=id%>">
        <h1>Dados do Atendimento</h1>
        <table>
            <tr>
                <td>Paciente <br/> <input type="text" id="txtNomePaciente" name="txtNomePaciente" value="<%=paciente%>" disabled /> </td>
                <td>Dt. Nascimento <br/> <input type="text" id="txtDataNascimento" name="txtDataNascimento" value="<%=data%>" disabled /> </td>
                <td>Responsável <br/> <input type="text" id="txtResponsavel" name="txtResponsavel" value="<%=responsavel%>" disabled /> </td>            
            </tr>
        </table>
        <h1>Dados da Avaliacao</h1>
        <table>
            <tr>
                <td colspan="2">
                    Principais sintomas: <br />
                    <textarea rows="5" cols="50" id="txtSintomas" name="txtSintomas"><%=sintomas%></textarea>
                </td>
                <td colspan="2">
                    Diagnotico inicial: <br />
                    <textarea rows="5" cols="50" id="txtDiagnostico" name="txtDiagnostico"><%=diagnostico%></textarea>
                </td>
            </tr>
            <tr>
                <td>
                    Internacao: <br />
                    <select id="ddlInternacao" name="ddlInternacao">
                        <option value="false">Nao</option>
                        <option value="true">Sim</option>
                    </select>
                </td>
                <td>
                    Emergencia: <br />
                    <select id="ddlEmergencia" name="ddlEmergencia">
                        <option value="false">Nao</option>
                        <option value="true">Sim</option>
                    </select>
                </td>
                <td>
                    Dispensado: <br />
                    <select id="ddlDispensado" name="ddlDispensado">
                        <option value="false">Nao</option>
                        <option value="true">Sim</option>
                    </select>
                </td>
                <td></td>
            </tr>
            <tr>
                <td colspan="2">
                    Carater internacao: <br />
                    <textarea rows="5" cols="50" id="txtCaraterInternacao" name="txtCaraterInternacao"><%=carater%></textarea>
                </td>
                <td colspan="2">
                    Condicao internacao: <br />
                    <textarea rows="5" cols="50" id="txtCondicaoInternacao" name="txtCondicaoInternacao"><%=condicao%></textarea>
                </td>
            </tr>
            <tr>
                <%
                    MedicoDAO daoMedico = new MedicoDAO();
                    List<Medico> medicos = daoMedico.selectAll();
                    DropDownList ddlMedico = new DropDownList("ddlMedico", "ddlMedico", new ArrayList<Object>(medicos), "getNome", "getCrm");
                    Usuario u = (Usuario) request.getSession(true).getAttribute("User");
                    boolean disabled = false;
                    if (u != null) {
                        if (u.getTipo() == 2) {
                            for (Medico m : medicos) {
                                if (m.getCrm() == Integer.parseInt(u.getLogin())) {
                                    ddlMedico.setSelectedIndex(medicos.indexOf(m));
                                    ddlMedico.disable();
                                    disabled = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!disabled) {
                        for (Medico m : medicos) {
                            if (m.getCrm().equals(crm)) {
                                ddlMedico.setSelectedIndex(medicos.indexOf(m));
                                ddlMedico.disable();
                                disabled = true;
                                break;
                            }
                        }
                    }
                %>
                <td>Médico<br /><%=ddlMedico.render()%></td>
                    <%
                        ProcedimentoDAO daoProcedimento = new ProcedimentoDAO();
                        DropDownList ddlProcedimento = new DropDownList("ddlProcedimento", "ddlProcedimento", new ArrayList<Object>(daoProcedimento.selectAll()), "getDescricao", "getCodigo");
                    %>
                <td>Procedimento <br /><%=ddlProcedimento.render()%></td>
                    <%
                        ClinicaDAO daoClinica = new ClinicaDAO();
                        DropDownList ddlClinica = new DropDownList("ddlClinica", "ddlClinica", new ArrayList<Object>(daoClinica.selectAll()), "getDescricao", "getCns");
                    %>
                <td>Clinica <br /><%=ddlClinica.render()%></td>
                <td></td>
            </tr>
        </table>
        <%
            if (exames != null) {
                if (!exames.isEmpty()) {
                    ArrayList<PersonalGridColumn> cols = new ArrayList<PersonalGridColumn>();
                    PersonalGridColumn c = new PersonalGridColumn("Exame", "getDescricaoExame", "");
                    c.setWidth(30);
                    cols.add(c);


                    c = new PersonalGridColumn("Resultado", "getResultado", "");
                    c.setWidth(70);
                    cols.add(c);

                    PersonalGridStyle rs = new PersonalGridStyle();

                    PersonalGrid g = new PersonalGrid("exames", "exames", new ArrayList<Object>(exames), cols, rs);
                    %>
                    <br />
                    <h1>Exames realizados</h1>
                    <%=g.render(null)%>
                    <br /> 
                    <%
                }
            }
        %>
        <br />
        <a class="botao" href="#" onclick="enviaForm('frmAvaliacao', '<%=servletUrl%>', 'erro');">Salvar</a>&nbsp;&nbsp;
        <a class="botao" href="<%=request.getContextPath() + "/relatorios/listagemAvaliacao.jsp"%>">Voltar </a>
    </form>

</div>
<jsp:include page="../rodape.jsp"></jsp:include>
